package org.example.day04;

import javax.swing.plaf.synth.SynthRadioButtonMenuItemUI;

/**
 * 第二章节2.2.4:一半同步，一半异步
 */
public class MyThread4 {
    public void longTask() {
        for (int i = 0; i < 100; i++) {
            try {
                System.out.println("ThreadName = " + Thread.currentThread().getName() + " ---- " + (i + 1));
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        synchronized (this) {
            for (int i = 0; i < 100; i++) {
                System.out.println("ThreadName = " + Thread.currentThread().getName() + " ★★★★ " + (i + 1));
            }
        }
    }

    public static void main(String[] args) {
        MyThread4 m4 = new MyThread4();
        Thread4A ta = new Thread4A(m4);
        ta.start();
        Thread4B tb = new Thread4B(m4);
        tb.start();
    }
}

class Thread4A extends Thread {
    private MyThread4 m4;

    public Thread4A(MyThread4 m4) {
        this.m4 = m4;
    }

    @Override
    public void run() {
        m4.longTask();
    }
}

class Thread4B extends Thread {
    private MyThread4 m4;

    public Thread4B(MyThread4 m4) {
        this.m4 = m4;
    }

    @Override
    public void run() {
        m4.longTask();
    }
}
